package de.tum.team26.eistpoll.client.junit;

import org.junit.BeforeClass;
import org.junit.Test;
import com.google.gwt.core.client.GWT;
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.user.client.rpc.AsyncCallback;
import de.tum.team26.eistpoll.client.PollService;
import de.tum.team26.eistpoll.client.PollServiceAsync;

/**
 * Class for JUnit Tests
 */
public class PollServiceTest extends GWTTestCase{
	// Attributs from the Tutorial
	private static PollServiceAsync pollServiceSvc;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		pollServiceSvc = GWT.create(PollService.class);
	}
	
	/**
	 * Check if Valid User is Accepted
	 */
	@Test
	public void testCheckUser() {
		// Initialize the service proxy.
		if (pollServiceSvc == null) {
			pollServiceSvc = GWT.create(PollService.class);
		}
		// Set up the callback object.
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
			public void onFailure(Throwable caught) {
			}
			@Override
			public void onSuccess(Boolean result) {
				assessEquals(true, result);
			}
		};
		// Call
		pollServiceSvc.checkUser("Prof", "pass", callback);
	}
	
	/**
	 * Assert to be Accessible within a Callback
	 * @param expected
	 * @param value
	 */
	private void assessEquals(boolean expected, boolean value){
		assertEquals(expected, value);
	}
	
	/**
	 * Check if not valid User is ignored
	 */
	@Test
	public void testCheckUserFail() {
		// Initialize the service proxy.
		if (pollServiceSvc == null) {
			pollServiceSvc = GWT.create(PollService.class);
		}
		// Set up the callback object.
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
			public void onFailure(Throwable caught) {
			}
			@Override
			public void onSuccess(Boolean result) {
				assessEquals(false, result);
			}
		};
		// Call
		pollServiceSvc.checkUser("xxx", "pass", callback);
	}
	
	/**
	 * Check if Push Service works correctly
	 */
	@Test
	public void testPushService() {
		// Initialize the service proxy.
		if (pollServiceSvc == null) {
			pollServiceSvc = GWT.create(PollService.class);
		}
		// Set up the callback object.
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
			public void onFailure(Throwable caught) {
			}
			@Override
			public void onSuccess(Boolean result) {
				assessEquals(false, result);
			}
		};
		// Call
		pollServiceSvc.pushService(callback);
	}

	/**
	 * Check if delete Polls work correctly
	 */
	@Test
	public void testDeletePoll() {
		// Initialize the service proxy.
		if (pollServiceSvc == null) {
			pollServiceSvc = GWT.create(PollService.class);
		}
		// Set up the callback object.
		AsyncCallback<Void> callback = new AsyncCallback<Void>() {
			public void onFailure(Throwable caught) {
			}
			@Override
			public void onSuccess(Void result) {
				assessEquals(true,true); // Callback indicates successful delete
			}
		};
		// Call
		pollServiceSvc.deletePoll(1, callback);
	}

	/**
	 * Test if Switch Poll State works correctly
	 */
	@Test
	public void testSwitchPollActive() {
		// Initialize the service proxy.
		if (pollServiceSvc == null) {
			pollServiceSvc = GWT.create(PollService.class);
		}
		// Set up the callback object.
		AsyncCallback<Void> callback = new AsyncCallback<Void>() {
			public void onFailure(Throwable caught) {

			}
			@Override
			public void onSuccess(Void result) {
				assessEquals(true,true); // Callback indicates successful delete
			}
		};
		// Call
		pollServiceSvc.switchPollActive(1, callback);
	}
	
	/**
	 * Return Module Name
	 */
	@Override
	public String getModuleName() {
		return "de.tum.team26.eistpoll.EIST_POLL";
	}

}
